package service

import (
	"github.com/apenella/go-ansible/v2/pkg/execute"
	"github.com/apenella/go-ansible/v2/pkg/playbook"
	"golang.org/x/net/context"
)

type V2PlayService struct {
}

func (s *V2PlayService) V2Play() error {
	// 1. 构造 playbook 选项
	playbookOptions := &playbook.AnsiblePlaybookOptions{
		Inventory: "../inventory.json",
	}

	// 2. 生成命令对象（v2 才有 NewAnsiblePlaybookCmd）
	playbookCmd := playbook.NewAnsiblePlaybookCmd(
		playbook.WithPlaybooks("../site.yml"),
		playbook.WithPlaybookOptions(playbookOptions),
	)

	// 3. 交给执行器
	exec := execute.NewDefaultExecute(
		execute.WithCmd(playbookCmd),
	)

	if err := exec.Execute(context.TODO()); err != nil {
		return err
	}
	return nil
}
